<!DOCTYPE html>



  


<html class="theme-next gemini use-motion" lang="zh-Hans">
<head>
  <meta charset="UTF-8"/>
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1"/>
<meta name="theme-color" content="#222">









<meta http-equiv="Cache-Control" content="no-transform" />
<meta http-equiv="Cache-Control" content="no-siteapp" />
















  
  
  <link href="/lib/fancybox/source/jquery.fancybox.css?v=2.1.5" rel="stylesheet" type="text/css" />







  

<link href="https://cdn.bootcss.com/font-awesome/4.6.2/css/font-awesome.min.css" rel="stylesheet" type="text/css" />

<link href="/css/main.css?v=5.1.4" rel="stylesheet" type="text/css" />


  <link rel="apple-touch-icon" sizes="180x180" href="/images/apple-touch-icon-next.png?v=5.1.4">


  <link rel="icon" type="image/png" sizes="32x32" href="/images/favicon-32x32-next.png?v=5.1.4">


  <link rel="icon" type="image/png" sizes="16x16" href="/images/favicon-16x16-next.png?v=5.1.4">


  <link rel="mask-icon" href="/images/logo.svg?v=5.1.4" color="#222">





  <meta name="keywords" content="C#,MySQLHelp," />










<meta name="description" content="前言以前做易语言/PHP的.  最近刚入门C#, 就简单的封装了一个类库, 边学边玩才容易学到东西嘛, 比起sqlserver, 我还是觉得mysql更加有亲切感;于是模仿ThinkPHP编写了一个”MySql”数据库操作类.    东西做的比较简单, 学习过程中也有大佬指导了一些问题. 在此非常感谢这个类库应该来说还有一些问题需要解决,  异常捕捉什么的.  不懂怎么弄.  欢迎热心大佬们指正调">
<meta name="keywords" content="C#,MySQLHelp">
<meta property="og:type" content="article">
<meta property="og:title" content="[C#] MySqlHelp类 DbModel.MySql 数据库操作类">
<meta property="og:url" content="http://edk24.com/2017/c4f36a06.html">
<meta property="og:site_name" content="余小波の技术博客">
<meta property="og:description" content="前言以前做易语言/PHP的.  最近刚入门C#, 就简单的封装了一个类库, 边学边玩才容易学到东西嘛, 比起sqlserver, 我还是觉得mysql更加有亲切感;于是模仿ThinkPHP编写了一个”MySql”数据库操作类.    东西做的比较简单, 学习过程中也有大佬指导了一些问题. 在此非常感谢这个类库应该来说还有一些问题需要解决,  异常捕捉什么的.  不懂怎么弄.  欢迎热心大佬们指正调">
<meta property="og:locale" content="zh-Hans">
<meta property="og:updated_time" content="2018-09-08T14:55:27.000Z">
<meta name="twitter:card" content="summary">
<meta name="twitter:title" content="[C#] MySqlHelp类 DbModel.MySql 数据库操作类">
<meta name="twitter:description" content="前言以前做易语言/PHP的.  最近刚入门C#, 就简单的封装了一个类库, 边学边玩才容易学到东西嘛, 比起sqlserver, 我还是觉得mysql更加有亲切感;于是模仿ThinkPHP编写了一个”MySql”数据库操作类.    东西做的比较简单, 学习过程中也有大佬指导了一些问题. 在此非常感谢这个类库应该来说还有一些问题需要解决,  异常捕捉什么的.  不懂怎么弄.  欢迎热心大佬们指正调">



<script type="text/javascript" id="hexo.configurations">
  var NexT = window.NexT || {};
  var CONFIG = {
    root: '/',
    scheme: 'Gemini',
    version: '5.1.4',
    sidebar: {"position":"left","display":"post","offset":12,"b2t":true,"scrollpercent":true,"onmobile":false},
    fancybox: true,
    tabs: true,
    motion: {"enable":true,"async":false,"transition":{"post_block":"fadeIn","post_header":"slideDownIn","post_body":"slideDownIn","coll_header":"slideLeftIn","sidebar":"slideUpIn"}},
    duoshuo: {
      userId: '0',
      author: '博主'
    },
    algolia: {
      applicationID: '',
      apiKey: '',
      indexName: '',
      hits: {"per_page":10},
      labels: {"input_placeholder":"Search for Posts","hits_empty":"We didn't find any results for the search: ${query}","hits_stats":"${hits} results found in ${time} ms"}
    }
  };
</script>



  <link rel="canonical" href="http://edk24.com/2017/c4f36a06.html"/>





  <title>[C#] MySqlHelp类 DbModel.MySql 数据库操作类 | 余小波の技术博客</title>
  








</head>

<body itemscope itemtype="http://schema.org/WebPage" lang="zh-Hans">

  
  
    
  

  <div class="container sidebar-position-left page-post-detail">
    <div class="headband"></div>

    <header id="header" class="header" itemscope itemtype="http://schema.org/WPHeader">
      <div class="header-inner"><div class="site-brand-wrapper">
  <div class="site-meta ">
    

    <div class="custom-logo-site-title">
      <a href="/"  class="brand" rel="start">
        <span class="logo-line-before"><i></i></span>
        <span class="site-title">余小波の技术博客</span>
        <span class="logo-line-after"><i></i></span>
      </a>
    </div>
      
        <h1 class="site-subtitle" itemprop="description">写代码的 会修电脑!</h1>
      
  </div>

  <div class="site-nav-toggle">
    <button>
      <span class="btn-bar"></span>
      <span class="btn-bar"></span>
      <span class="btn-bar"></span>
    </button>
  </div>
</div>

<nav class="site-nav">
  

  
    <ul id="menu" class="menu">
      
        
        <li class="menu-item menu-item-home">
          <a href="/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-home"></i> <br />
            
            首页
          </a>
        </li>
      
        
        <li class="menu-item menu-item-about">
          <a href="/about/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-user"></i> <br />
            
            关于
          </a>
        </li>
      
        
        <li class="menu-item menu-item-tags">
          <a href="/tags/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-tags"></i> <br />
            
            标签
          </a>
        </li>
      
        
        <li class="menu-item menu-item-categories">
          <a href="/categories/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-th"></i> <br />
            
            分类
          </a>
        </li>
      
        
        <li class="menu-item menu-item-archives">
          <a href="/archives/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-archive"></i> <br />
            
            归档
          </a>
        </li>
      

      
        <li class="menu-item menu-item-search">
          
            <a href="javascript:;" class="popup-trigger">
          
            
              <i class="menu-item-icon fa fa-search fa-fw"></i> <br />
            
            搜索
          </a>
        </li>
      
    </ul>
  

  
    <div class="site-search">
      
  <div class="popup search-popup local-search-popup">
  <div class="local-search-header clearfix">
    <span class="search-icon">
      <i class="fa fa-search"></i>
    </span>
    <span class="popup-btn-close">
      <i class="fa fa-times-circle"></i>
    </span>
    <div class="local-search-input-wrapper">
      <input autocomplete="off"
             placeholder="搜索..." spellcheck="false"
             type="text" id="local-search-input">
    </div>
  </div>
  <div id="local-search-result"></div>
</div>



    </div>
  
</nav>



 </div>
    </header>

    <main id="main" class="main">
      <div class="main-inner">
        <div class="content-wrap">
          <div id="content" class="content">
            

  <div id="posts" class="posts-expand">
    

  

  
  
  

  <article class="post post-type-normal" itemscope itemtype="http://schema.org/Article">
  
  
  
  <div class="post-block">
    <link itemprop="mainEntityOfPage" href="http://edk24.com/2017/c4f36a06.html">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="name" content="余小波">
      <meta itemprop="description" content="">
      <meta itemprop="image" content="/images/avatar.png">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="余小波の技术博客">
    </span>

    
      <header class="post-header">

        
        
          <h2 class="post-title" itemprop="name headline">[C#] MySqlHelp类 DbModel.MySql 数据库操作类</h2>
        

        <div class="post-meta">
          <span class="post-time">
            
              <span class="post-meta-item-icon">
                <i class="fa fa-calendar-o"></i>
              </span>
              
                <span class="post-meta-item-text">发表于</span>
              
              <time title="创建于" itemprop="dateCreated datePublished" datetime="2017-09-22T22:46:49+08:00">
                2017-09-22
              </time>
            

            

            
          </span>

          
            <span class="post-category" >
            
              <span class="post-meta-divider">|</span>
            
              <span class="post-meta-item-icon">
                <i class="fa fa-folder-o"></i>
              </span>
              
                <span class="post-meta-item-text">分类于</span>
              
              
                <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
                  <a href="/categories/C/" itemprop="url" rel="index">
                    <span itemprop="name">C#</span>
                  </a>
                </span>

                
                
                  ， 
                
              
                <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
                  <a href="/categories/C/类库/" itemprop="url" rel="index">
                    <span itemprop="name">类库</span>
                  </a>
                </span>

                
                
              
            </span>
          

          
            
              <span class="post-comments-count">
                <span class="post-meta-divider">|</span>
                <span class="post-meta-item-icon">
                  <i class="fa fa-comment-o"></i>
                </span>
                <a href="/2017/c4f36a06.html#comments" itemprop="discussionUrl">
                  <span class="post-comments-count gitment-comments-count" data-xid="/2017/c4f36a06.html" itemprop="commentsCount"></span>
                </a>
              </span>
            
          

          
          

          

          

          

        </div>
      </header>
    

    
    
    
    <div class="post-body" itemprop="articleBody">

      
      

      
        <h1 id="前言"><a href="#前言" class="headerlink" title="前言"></a>前言</h1><p>以前做易语言/PHP的.  最近刚入门C#, 就简单的封装了一个类库, 边学边玩才容易学到东西嘛, 比起sqlserver, 我还是觉得mysql更加有亲切感;<br>于是模仿ThinkPHP编写了一个”MySql”数据库操作类.   </p>
<p><strong>东西做的比较简单, 学习过程中也有大佬指导了一些问题. 在此非常感谢</strong><br><strong>这个类库应该来说还有一些问题需要解决,  异常捕捉什么的.  不懂怎么弄.  欢迎热心大佬们指正调教</strong></p>
<p><strong><em>此类库用到了 MySql.Data.dll</em></strong></p>
<ul>
<li><a href="https://pan.baidu.com/s/1nvebMZn" target="_blank" rel="noopener">下载mysql.data.dll</a> </li>
<li><a href="https://pan.baidu.com/s/1jIEFLZo" target="_blank" rel="noopener">下载完整项目</a><a id="more"></a>
<h1 id="数据表"><a href="#数据表" class="headerlink" title="数据表"></a>数据表</h1><figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">DROP</span> <span class="keyword">TABLE</span> <span class="keyword">IF</span> <span class="keyword">EXISTS</span> <span class="string">`table_user`</span>;</span><br><span class="line"><span class="keyword">CREATE</span> <span class="keyword">TABLE</span> <span class="string">`table_user`</span> (</span><br><span class="line">  <span class="string">`userid`</span> <span class="built_in">int</span>(<span class="number">11</span>) <span class="keyword">NOT</span> <span class="literal">NULL</span> AUTO_INCREMENT,</span><br><span class="line">  <span class="string">`username`</span> <span class="built_in">varchar</span>(<span class="number">255</span>) <span class="keyword">NOT</span> <span class="literal">NULL</span>,</span><br><span class="line">  <span class="string">`password`</span> <span class="built_in">varchar</span>(<span class="number">255</span>) <span class="keyword">NOT</span> <span class="literal">NULL</span>,</span><br><span class="line">  PRIMARY <span class="keyword">KEY</span> (<span class="string">`userid`</span>)</span><br><span class="line">)</span><br></pre></td></tr></table></figure>
</li>
</ul>
<h1 id="类库代码"><a href="#类库代码" class="headerlink" title="类库代码"></a>类库代码</h1><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br><span class="line">82</span><br><span class="line">83</span><br><span class="line">84</span><br><span class="line">85</span><br><span class="line">86</span><br><span class="line">87</span><br><span class="line">88</span><br><span class="line">89</span><br><span class="line">90</span><br><span class="line">91</span><br><span class="line">92</span><br><span class="line">93</span><br><span class="line">94</span><br><span class="line">95</span><br><span class="line">96</span><br><span class="line">97</span><br><span class="line">98</span><br><span class="line">99</span><br><span class="line">100</span><br><span class="line">101</span><br><span class="line">102</span><br><span class="line">103</span><br><span class="line">104</span><br><span class="line">105</span><br><span class="line">106</span><br><span class="line">107</span><br><span class="line">108</span><br><span class="line">109</span><br><span class="line">110</span><br><span class="line">111</span><br><span class="line">112</span><br><span class="line">113</span><br><span class="line">114</span><br><span class="line">115</span><br><span class="line">116</span><br><span class="line">117</span><br><span class="line">118</span><br><span class="line">119</span><br><span class="line">120</span><br><span class="line">121</span><br><span class="line">122</span><br><span class="line">123</span><br><span class="line">124</span><br><span class="line">125</span><br><span class="line">126</span><br><span class="line">127</span><br><span class="line">128</span><br><span class="line">129</span><br><span class="line">130</span><br><span class="line">131</span><br><span class="line">132</span><br><span class="line">133</span><br><span class="line">134</span><br><span class="line">135</span><br><span class="line">136</span><br><span class="line">137</span><br><span class="line">138</span><br><span class="line">139</span><br><span class="line">140</span><br><span class="line">141</span><br><span class="line">142</span><br><span class="line">143</span><br><span class="line">144</span><br><span class="line">145</span><br><span class="line">146</span><br><span class="line">147</span><br><span class="line">148</span><br><span class="line">149</span><br><span class="line">150</span><br><span class="line">151</span><br><span class="line">152</span><br><span class="line">153</span><br><span class="line">154</span><br><span class="line">155</span><br><span class="line">156</span><br><span class="line">157</span><br><span class="line">158</span><br><span class="line">159</span><br><span class="line">160</span><br><span class="line">161</span><br><span class="line">162</span><br><span class="line">163</span><br><span class="line">164</span><br><span class="line">165</span><br><span class="line">166</span><br><span class="line">167</span><br><span class="line">168</span><br><span class="line">169</span><br><span class="line">170</span><br><span class="line">171</span><br><span class="line">172</span><br><span class="line">173</span><br><span class="line">174</span><br><span class="line">175</span><br><span class="line">176</span><br><span class="line">177</span><br><span class="line">178</span><br><span class="line">179</span><br><span class="line">180</span><br><span class="line">181</span><br><span class="line">182</span><br><span class="line">183</span><br><span class="line">184</span><br><span class="line">185</span><br><span class="line">186</span><br><span class="line">187</span><br><span class="line">188</span><br><span class="line">189</span><br><span class="line">190</span><br><span class="line">191</span><br><span class="line">192</span><br><span class="line">193</span><br><span class="line">194</span><br><span class="line">195</span><br><span class="line">196</span><br><span class="line">197</span><br><span class="line">198</span><br><span class="line">199</span><br><span class="line">200</span><br><span class="line">201</span><br><span class="line">202</span><br><span class="line">203</span><br><span class="line">204</span><br><span class="line">205</span><br><span class="line">206</span><br><span class="line">207</span><br><span class="line">208</span><br><span class="line">209</span><br><span class="line">210</span><br><span class="line">211</span><br><span class="line">212</span><br><span class="line">213</span><br><span class="line">214</span><br><span class="line">215</span><br><span class="line">216</span><br><span class="line">217</span><br><span class="line">218</span><br><span class="line">219</span><br><span class="line">220</span><br><span class="line">221</span><br><span class="line">222</span><br><span class="line">223</span><br><span class="line">224</span><br><span class="line">225</span><br><span class="line">226</span><br><span class="line">227</span><br><span class="line">228</span><br><span class="line">229</span><br><span class="line">230</span><br><span class="line">231</span><br><span class="line">232</span><br><span class="line">233</span><br><span class="line">234</span><br><span class="line">235</span><br><span class="line">236</span><br><span class="line">237</span><br><span class="line">238</span><br><span class="line">239</span><br><span class="line">240</span><br><span class="line">241</span><br><span class="line">242</span><br><span class="line">243</span><br><span class="line">244</span><br><span class="line">245</span><br><span class="line">246</span><br><span class="line">247</span><br><span class="line">248</span><br><span class="line">249</span><br><span class="line">250</span><br><span class="line">251</span><br><span class="line">252</span><br><span class="line">253</span><br><span class="line">254</span><br><span class="line">255</span><br><span class="line">256</span><br><span class="line">257</span><br><span class="line">258</span><br><span class="line">259</span><br><span class="line">260</span><br><span class="line">261</span><br><span class="line">262</span><br><span class="line">263</span><br><span class="line">264</span><br><span class="line">265</span><br><span class="line">266</span><br><span class="line">267</span><br><span class="line">268</span><br><span class="line">269</span><br><span class="line">270</span><br><span class="line">271</span><br><span class="line">272</span><br><span class="line">273</span><br><span class="line">274</span><br><span class="line">275</span><br><span class="line">276</span><br><span class="line">277</span><br><span class="line">278</span><br><span class="line">279</span><br><span class="line">280</span><br><span class="line">281</span><br><span class="line">282</span><br><span class="line">283</span><br><span class="line">284</span><br><span class="line">285</span><br><span class="line">286</span><br><span class="line">287</span><br><span class="line">288</span><br><span class="line">289</span><br><span class="line">290</span><br><span class="line">291</span><br><span class="line">292</span><br><span class="line">293</span><br><span class="line">294</span><br><span class="line">295</span><br><span class="line">296</span><br><span class="line">297</span><br><span class="line">298</span><br><span class="line">299</span><br><span class="line">300</span><br><span class="line">301</span><br><span class="line">302</span><br><span class="line">303</span><br><span class="line">304</span><br><span class="line">305</span><br><span class="line">306</span><br><span class="line">307</span><br><span class="line">308</span><br><span class="line">309</span><br><span class="line">310</span><br><span class="line">311</span><br><span class="line">312</span><br><span class="line">313</span><br><span class="line">314</span><br><span class="line">315</span><br><span class="line">316</span><br><span class="line">317</span><br><span class="line">318</span><br><span class="line">319</span><br><span class="line">320</span><br><span class="line">321</span><br><span class="line">322</span><br><span class="line">323</span><br><span class="line">324</span><br><span class="line">325</span><br><span class="line">326</span><br><span class="line">327</span><br><span class="line">328</span><br><span class="line">329</span><br><span class="line">330</span><br><span class="line">331</span><br><span class="line">332</span><br><span class="line">333</span><br><span class="line">334</span><br><span class="line">335</span><br><span class="line">336</span><br><span class="line">337</span><br><span class="line">338</span><br><span class="line">339</span><br><span class="line">340</span><br><span class="line">341</span><br><span class="line">342</span><br><span class="line">343</span><br><span class="line">344</span><br><span class="line">345</span><br><span class="line">346</span><br><span class="line">347</span><br><span class="line">348</span><br><span class="line">349</span><br><span class="line">350</span><br><span class="line">351</span><br><span class="line">352</span><br><span class="line">353</span><br><span class="line">354</span><br><span class="line">355</span><br><span class="line">356</span><br><span class="line">357</span><br><span class="line">358</span><br><span class="line">359</span><br><span class="line">360</span><br><span class="line">361</span><br><span class="line">362</span><br><span class="line">363</span><br><span class="line">364</span><br><span class="line">365</span><br><span class="line">366</span><br><span class="line">367</span><br><span class="line">368</span><br><span class="line">369</span><br><span class="line">370</span><br><span class="line">371</span><br><span class="line">372</span><br><span class="line">373</span><br><span class="line">374</span><br><span class="line">375</span><br><span class="line">376</span><br><span class="line">377</span><br><span class="line">378</span><br><span class="line">379</span><br><span class="line">380</span><br><span class="line">381</span><br><span class="line">382</span><br><span class="line">383</span><br><span class="line">384</span><br><span class="line">385</span><br><span class="line">386</span><br><span class="line">387</span><br><span class="line">388</span><br><span class="line">389</span><br><span class="line">390</span><br><span class="line">391</span><br><span class="line">392</span><br><span class="line">393</span><br><span class="line">394</span><br><span class="line">395</span><br><span class="line">396</span><br><span class="line">397</span><br><span class="line">398</span><br><span class="line">399</span><br><span class="line">400</span><br><span class="line">401</span><br><span class="line">402</span><br><span class="line">403</span><br><span class="line">404</span><br><span class="line">405</span><br><span class="line">406</span><br><span class="line">407</span><br><span class="line">408</span><br><span class="line">409</span><br><span class="line">410</span><br><span class="line">411</span><br><span class="line">412</span><br><span class="line">413</span><br><span class="line">414</span><br><span class="line">415</span><br><span class="line">416</span><br><span class="line">417</span><br><span class="line">418</span><br><span class="line">419</span><br><span class="line">420</span><br><span class="line">421</span><br><span class="line">422</span><br><span class="line">423</span><br><span class="line">424</span><br><span class="line">425</span><br><span class="line">426</span><br><span class="line">427</span><br><span class="line">428</span><br><span class="line">429</span><br><span class="line">430</span><br><span class="line">431</span><br><span class="line">432</span><br><span class="line">433</span><br><span class="line">434</span><br><span class="line">435</span><br><span class="line">436</span><br><span class="line">437</span><br><span class="line">438</span><br><span class="line">439</span><br><span class="line">440</span><br><span class="line">441</span><br><span class="line">442</span><br><span class="line">443</span><br><span class="line">444</span><br><span class="line">445</span><br><span class="line">446</span><br><span class="line">447</span><br><span class="line">448</span><br><span class="line">449</span><br><span class="line">450</span><br><span class="line">451</span><br><span class="line">452</span><br><span class="line">453</span><br><span class="line">454</span><br><span class="line">455</span><br><span class="line">456</span><br><span class="line">457</span><br><span class="line">458</span><br><span class="line">459</span><br><span class="line">460</span><br><span class="line">461</span><br><span class="line">462</span><br><span class="line">463</span><br><span class="line">464</span><br><span class="line">465</span><br><span class="line">466</span><br><span class="line">467</span><br><span class="line">468</span><br><span class="line">469</span><br><span class="line">470</span><br><span class="line">471</span><br><span class="line">472</span><br><span class="line">473</span><br><span class="line">474</span><br><span class="line">475</span><br><span class="line">476</span><br><span class="line">477</span><br><span class="line">478</span><br><span class="line">479</span><br><span class="line">480</span><br><span class="line">481</span><br><span class="line">482</span><br><span class="line">483</span><br><span class="line">484</span><br><span class="line">485</span><br><span class="line">486</span><br><span class="line">487</span><br><span class="line">488</span><br><span class="line">489</span><br><span class="line">490</span><br><span class="line">491</span><br><span class="line">492</span><br><span class="line">493</span><br><span class="line">494</span><br><span class="line">495</span><br><span class="line">496</span><br><span class="line">497</span><br><span class="line">498</span><br><span class="line">499</span><br><span class="line">500</span><br><span class="line">501</span><br><span class="line">502</span><br><span class="line">503</span><br><span class="line">504</span><br><span class="line">505</span><br><span class="line">506</span><br><span class="line">507</span><br><span class="line">508</span><br><span class="line">509</span><br><span class="line">510</span><br><span class="line">511</span><br><span class="line">512</span><br><span class="line">513</span><br><span class="line">514</span><br><span class="line">515</span><br><span class="line">516</span><br><span class="line">517</span><br><span class="line">518</span><br><span class="line">519</span><br><span class="line">520</span><br><span class="line">521</span><br><span class="line">522</span><br><span class="line">523</span><br><span class="line">524</span><br><span class="line">525</span><br><span class="line">526</span><br><span class="line">527</span><br><span class="line">528</span><br><span class="line">529</span><br><span class="line">530</span><br><span class="line">531</span><br><span class="line">532</span><br><span class="line">533</span><br><span class="line">534</span><br><span class="line">535</span><br><span class="line">536</span><br><span class="line">537</span><br><span class="line">538</span><br><span class="line">539</span><br><span class="line">540</span><br><span class="line">541</span><br><span class="line">542</span><br><span class="line">543</span><br><span class="line">544</span><br><span class="line">545</span><br><span class="line">546</span><br><span class="line">547</span><br><span class="line">548</span><br><span class="line">549</span><br><span class="line">550</span><br><span class="line">551</span><br><span class="line">552</span><br><span class="line">553</span><br><span class="line">554</span><br><span class="line">555</span><br><span class="line">556</span><br><span class="line">557</span><br><span class="line">558</span><br><span class="line">559</span><br><span class="line">560</span><br><span class="line">561</span><br><span class="line">562</span><br><span class="line">563</span><br><span class="line">564</span><br><span class="line">565</span><br><span class="line">566</span><br><span class="line">567</span><br><span class="line">568</span><br><span class="line">569</span><br><span class="line">570</span><br><span class="line">571</span><br><span class="line">572</span><br><span class="line">573</span><br><span class="line">574</span><br><span class="line">575</span><br><span class="line">576</span><br></pre></td><td class="code"><pre><span class="line">using System;</span><br><span class="line">using System.Collections;</span><br><span class="line">using MySql.Data.MySqlClient;</span><br><span class="line">using System.Data;</span><br><span class="line"></span><br><span class="line">namespace DbModel</span><br><span class="line">&#123;</span><br><span class="line">    public class MySQL</span><br><span class="line">    &#123;</span><br><span class="line">        protected string m_table, m_where, m_field = &quot;*&quot;, m_limit, m_group, m_order, m_having, m_alias, m_prefix, m_comment, m_distinct;</span><br><span class="line">        protected bool m_fetchSql = false;</span><br><span class="line">        public string LastSql = &quot;&quot;; //最后生成的SQL</span><br><span class="line"></span><br><span class="line">        protected ArrayList m_join = new ArrayList(), m_union = new ArrayList();</span><br><span class="line">        protected Hashtable m_data = new Hashtable();</span><br><span class="line">        </span><br><span class="line">        public Int64 AffectCount &#123; get; protected set; &#125;</span><br><span class="line">        public Int64 RecordCount &#123; get; protected set; &#125;</span><br><span class="line">        public Int64 FieldCount &#123; get; protected set; &#125;</span><br><span class="line"></span><br><span class="line">        public string ConnectionString;</span><br><span class="line"></span><br><span class="line">        public MySqlConnection Connection;</span><br><span class="line">        public MySqlCommand Command;</span><br><span class="line">        public string Exception;</span><br><span class="line"></span><br><span class="line">        /// &lt;summary&gt;</span><br><span class="line">        /// 构造函数</span><br><span class="line">        /// &lt;/summary&gt;</span><br><span class="line">        /// &lt;param name=&quot;table&quot;&gt;&lt;/param&gt;</span><br><span class="line">        /// &lt;param name=&quot;prefix&quot;&gt;&lt;/param&gt;</span><br><span class="line">        public MySQL(string table, string prefix = &quot;&quot;)</span><br><span class="line">        &#123;</span><br><span class="line">            this.m_table = table;</span><br><span class="line">            this.m_prefix = prefix;</span><br><span class="line">        &#125;</span><br><span class="line"></span><br><span class="line"></span><br><span class="line">        /// &lt;summary&gt;</span><br><span class="line">        /// 置表名</span><br><span class="line">        /// &lt;/summary&gt;</span><br><span class="line">        /// &lt;param name=&quot;table&quot;&gt;&lt;/param&gt;</span><br><span class="line">        /// &lt;param name=&quot;alias&quot;&gt;default null&lt;/param&gt;</span><br><span class="line">        /// &lt;returns&gt;DbModel&lt;/returns&gt;</span><br><span class="line">        public MySQL Table(String table, String alias = null)</span><br><span class="line">        &#123;</span><br><span class="line">            this.m_table = table;</span><br><span class="line">            if (alias != null)</span><br><span class="line">            &#123;</span><br><span class="line">                this.m_alias = &quot; AS &quot;+alias;</span><br><span class="line">            &#125;</span><br><span class="line">            return this;</span><br><span class="line">        &#125;</span><br><span class="line"></span><br><span class="line"></span><br><span class="line">        /// &lt;summary&gt;</span><br><span class="line">        /// 置选取字段   </span><br><span class="line">        /// &lt;/summary&gt;</span><br><span class="line">        /// &lt;param name=&quot;field&quot;&gt;&lt;/param&gt;</span><br><span class="line">        /// &lt;returns&gt;DbModel&lt;/returns&gt;</span><br><span class="line">        public MySQL Field(String field = &quot;*&quot;)</span><br><span class="line">        &#123;</span><br><span class="line">            this.m_field = field;</span><br><span class="line">            return this;</span><br><span class="line">        &#125;</span><br><span class="line"> </span><br><span class="line"> </span><br><span class="line">        /// &lt;summary&gt;</span><br><span class="line">        /// 设置表别名</span><br><span class="line">        /// &lt;/summary&gt;</span><br><span class="line">        /// &lt;param name=&quot;alias&quot;&gt;&lt;/param&gt;</span><br><span class="line">        /// &lt;returns&gt;&lt;/returns&gt;</span><br><span class="line">        public MySQL Alias(String alias)</span><br><span class="line">        &#123;</span><br><span class="line">            this.m_alias = &quot; AS &quot; + alias;</span><br><span class="line">            return this;</span><br><span class="line">        &#125;</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"></span><br><span class="line">        /// &lt;summary&gt;</span><br><span class="line">        /// 查询条件</span><br><span class="line">        /// &lt;/summary&gt;</span><br><span class="line">        /// &lt;param name=&quot;where&quot;&gt;&lt;/param&gt;</span><br><span class="line">        /// &lt;returns&gt;&lt;/returns&gt;</span><br><span class="line">        public MySQL Where(String where)</span><br><span class="line">        &#123;</span><br><span class="line">            this.m_where = where;</span><br><span class="line">            return this;</span><br><span class="line">        &#125;</span><br><span class="line"></span><br><span class="line">        /// &lt;summary&gt;</span><br><span class="line">        /// 排序方法</span><br><span class="line">        /// &lt;/summary&gt;</span><br><span class="line">        /// &lt;param name=&quot;order&quot;&gt;如 user_id [desc/asc]&lt;/param&gt;</span><br><span class="line">        /// &lt;returns&gt;&lt;/returns&gt;</span><br><span class="line">        public MySQL Order(String order)</span><br><span class="line">        &#123;</span><br><span class="line">            this.m_order = &quot; ORDER BY &quot; + order;</span><br><span class="line">            return this;</span><br><span class="line">        &#125;</span><br><span class="line"></span><br><span class="line">        /// &lt;summary&gt;</span><br><span class="line">        /// 设置表名前缀， 对所有表有效。如果前缀是“table_”那么需要加前缀的表前后加两个下划线“__user__”，就会得到“table_user”</span><br><span class="line">        /// &lt;/summary&gt;</span><br><span class="line">        /// &lt;param name=&quot;prefix&quot;&gt;&lt;/param&gt;</span><br><span class="line">        /// &lt;returns&gt;&lt;/returns&gt;</span><br><span class="line">        public MySQL Prefix(String prefix)</span><br><span class="line">        &#123;</span><br><span class="line">            this.m_prefix = prefix;</span><br><span class="line">            return this;</span><br><span class="line">        &#125;</span><br><span class="line"></span><br><span class="line">        /// &lt;summary&gt;</span><br><span class="line">        /// 选取部分数据</span><br><span class="line">        /// &lt;/summary&gt;</span><br><span class="line">        /// &lt;param name=&quot;start&quot;&gt;开始位置，默认为第一条, 如果留空参数2则直接&quot;LIMIT start&quot;&lt;/param&gt;</span><br><span class="line">        /// &lt;param name=&quot;num&quot;&gt;选取数量， 默认为0&lt;/param&gt;</span><br><span class="line">        /// &lt;returns&gt;&lt;/returns&gt;</span><br><span class="line">        public MySQL Limit(int start = 0, int num = 0)</span><br><span class="line">        &#123;</span><br><span class="line">            if (num == 0)</span><br><span class="line">            &#123;</span><br><span class="line">                this.m_limit = &quot; LIMIT &quot; + Convert.ToString(start);</span><br><span class="line">            &#125;</span><br><span class="line">            else</span><br><span class="line">            &#123;</span><br><span class="line">                this.m_limit = &quot; LIMIT &quot; + Convert.ToString(start) + &quot;, &quot; + Convert.ToString(num);</span><br><span class="line">            &#125;</span><br><span class="line">            return this;</span><br><span class="line">        &#125;</span><br><span class="line"></span><br><span class="line">        /// &lt;summary&gt;</span><br><span class="line">        /// 分页，本方法等同limit</span><br><span class="line">        /// &lt;/summary&gt;</span><br><span class="line">        /// &lt;param name=&quot;page&quot;&gt;第N页&lt;/param&gt;</span><br><span class="line">        /// &lt;param name=&quot;showNum&quot;&gt;每页显示数量&lt;/param&gt;</span><br><span class="line">        /// &lt;returns&gt;&lt;/returns&gt;</span><br><span class="line">        public MySQL Page(int page = 1, int showNum = 1000)</span><br><span class="line">        &#123;</span><br><span class="line">            this.Limit((page-1) * showNum, showNum);</span><br><span class="line">            return this;</span><br><span class="line">        &#125;</span><br><span class="line"></span><br><span class="line">        /// &lt;summary&gt;</span><br><span class="line">        /// 分组条件（相当于where），配合group使用（group by）</span><br><span class="line">        /// &lt;/summary&gt;</span><br><span class="line">        /// &lt;param name=&quot;having&quot;&gt;&lt;/param&gt;</span><br><span class="line">        /// &lt;returns&gt;&lt;/returns&gt;</span><br><span class="line">        public MySQL Having(String having)</span><br><span class="line">        &#123;</span><br><span class="line">            this.m_having = &quot; HAVING &quot; + having;</span><br><span class="line">            return this;</span><br><span class="line">        &#125;</span><br><span class="line"></span><br><span class="line">        /// &lt;summary&gt;</span><br><span class="line">        /// 分组</span><br><span class="line">        /// &lt;/summary&gt;</span><br><span class="line">        /// &lt;param name=&quot;group&quot;&gt;&lt;/param&gt;</span><br><span class="line">        /// &lt;returns&gt;&lt;/returns&gt;</span><br><span class="line">        public MySQL Group(String group)</span><br><span class="line">        &#123;</span><br><span class="line">            this.m_group = &quot; GROUP BY &quot; + group;</span><br><span class="line">            return this;</span><br><span class="line">        &#125;</span><br><span class="line"></span><br><span class="line">        /// &lt;summary&gt;</span><br><span class="line">        /// 连接多条SQL(多条SQL查询结果合并)</span><br><span class="line">        /// &lt;/summary&gt;</span><br><span class="line">        /// &lt;param name=&quot;sql&quot;&gt;&lt;/param&gt;</span><br><span class="line">        /// &lt;param name=&quot;all&quot;&gt;是否保留全部, 默认false (union / union all)&lt;/param&gt;</span><br><span class="line">        /// &lt;param name=&quot;clear&quot;&gt;是否清空&lt;/param&gt;</span><br><span class="line">        /// &lt;returns&gt;&lt;/returns&gt;</span><br><span class="line">        public MySQL Union(string sql, bool all = false, bool clear = false)</span><br><span class="line">        &#123;</span><br><span class="line">            if (clear)</span><br><span class="line">            &#123;</span><br><span class="line">                this.m_union.Clear();</span><br><span class="line">            &#125;</span><br><span class="line">            this.m_union.Add((all == true ? &quot;union all &quot; : &quot;union &quot;) + sql);</span><br><span class="line">            return this;</span><br><span class="line">        &#125;</span><br><span class="line"></span><br><span class="line">        /// &lt;summary&gt;</span><br><span class="line">        /// 关联多个表</span><br><span class="line">        /// &lt;/summary&gt;</span><br><span class="line">        /// &lt;param name=&quot;join&quot;&gt;例：LEFT JOIN __user__ on 1 = 1&lt;/param&gt;</span><br><span class="line">        /// &lt;param name=&quot;clear&quot;&gt;是否清除原先的join&lt;/param&gt;</span><br><span class="line">        /// &lt;returns&gt;&lt;/returns&gt;</span><br><span class="line">        public MySQL Join(String join, bool clear = false)</span><br><span class="line">        &#123;</span><br><span class="line">            if (clear)</span><br><span class="line">            &#123;</span><br><span class="line">                this.m_join.Clear();</span><br><span class="line">            &#125;</span><br><span class="line">            this.m_join.Add(join);</span><br><span class="line">            return this;</span><br><span class="line">        &#125;</span><br><span class="line"></span><br><span class="line">        /// &lt;summary&gt;</span><br><span class="line">        /// 注释</span><br><span class="line">        /// &lt;/summary&gt;</span><br><span class="line">        /// &lt;param name=&quot;text&quot;&gt;&lt;/param&gt;</span><br><span class="line">        /// &lt;returns&gt;&lt;/returns&gt;</span><br><span class="line">        public MySQL Comment(string text)</span><br><span class="line">        &#123;</span><br><span class="line">            this.m_comment = &quot; /* &quot; + text + &quot; */ &quot;;</span><br><span class="line">            return this;</span><br><span class="line">        &#125;</span><br><span class="line"></span><br><span class="line">        /// &lt;summary&gt;</span><br><span class="line">        /// 唯一</span><br><span class="line">        /// &lt;/summary&gt;</span><br><span class="line">        /// &lt;param name=&quot;distinct&quot;&gt;&lt;/param&gt;</span><br><span class="line">        /// &lt;returns&gt;&lt;/returns&gt;</span><br><span class="line">        public MySQL Distinct(bool distinct)</span><br><span class="line">        &#123;</span><br><span class="line">            this.m_distinct = (distinct == false ? &quot;&quot; : &quot;DISTINCT &quot;);</span><br><span class="line">            return this;</span><br><span class="line">        &#125;</span><br><span class="line"></span><br><span class="line">        /// &lt;summary&gt;</span><br><span class="line">        /// 获取SQL, 如果为True.   Select/Add/Save/Del 将返回SQL语句, 并且不执行</span><br><span class="line">        /// &lt;/summary&gt;</span><br><span class="line">        /// &lt;param name=&quot;fetch&quot;&gt;&lt;/param&gt;</span><br><span class="line">        /// &lt;returns&gt;&lt;/returns&gt;</span><br><span class="line">        public MySQL FetchSql(bool fetch)</span><br><span class="line">        &#123;</span><br><span class="line">            this.m_fetchSql = fetch;</span><br><span class="line">            return this;</span><br><span class="line">        &#125;</span><br><span class="line"></span><br><span class="line">        /// &lt;summary&gt;</span><br><span class="line">        /// 绑定字段</span><br><span class="line">        /// &lt;/summary&gt;</span><br><span class="line">        /// &lt;param name=&quot;field&quot;&gt;&lt;/param&gt;</span><br><span class="line">        /// &lt;param name=&quot;value&quot;&gt;&lt;/param&gt;</span><br><span class="line">        public MySQL Data(string field, object value)</span><br><span class="line">        &#123;</span><br><span class="line">            string temp = &quot;&quot;;</span><br><span class="line">            if (value == null)</span><br><span class="line">            &#123;</span><br><span class="line">                temp = &quot;null&quot;;</span><br><span class="line">            &#125;</span><br><span class="line">            else</span><br><span class="line">            &#123; </span><br><span class="line">                switch (value.GetType().Name)</span><br><span class="line">                &#123;</span><br><span class="line">                    case &quot;String&quot;:</span><br><span class="line">                        temp = &quot;&apos;&quot; + this.FilterSQL(value.ToString()) + &quot;&apos;&quot;;</span><br><span class="line">                        break;</span><br><span class="line">                    case &quot;DateTime&quot;:</span><br><span class="line">                        temp = &quot;&apos;&quot; + value.ToString() + &quot;&apos;&quot;;</span><br><span class="line">                        break;</span><br><span class="line"></span><br><span class="line">                    default:</span><br><span class="line">                        temp = value.ToString();</span><br><span class="line">                        break;</span><br><span class="line">                &#125;</span><br><span class="line">            &#125;</span><br><span class="line"></span><br><span class="line">            //如果存在:删除</span><br><span class="line">            if (this.m_data.ContainsKey(field))</span><br><span class="line">            &#123;</span><br><span class="line">                this.m_data.Remove(field);</span><br><span class="line">                this.m_data.Add(field, temp);</span><br><span class="line">                return this;</span><br><span class="line">            &#125;</span><br><span class="line"></span><br><span class="line">            //新增</span><br><span class="line"></span><br><span class="line">            this.m_data.Add(field, temp);</span><br><span class="line"></span><br><span class="line">            </span><br><span class="line">            return this;</span><br><span class="line">        &#125;</span><br><span class="line"></span><br><span class="line">        /// &lt;summary&gt;</span><br><span class="line">        /// 过滤SQL</span><br><span class="line">        /// &lt;/summary&gt;</span><br><span class="line">        /// &lt;param name=&quot;value&quot;&gt;&lt;/param&gt;</span><br><span class="line">        /// &lt;returns&gt;&lt;/returns&gt;</span><br><span class="line">        private string FilterSQL(string value)</span><br><span class="line">        &#123;</span><br><span class="line">            value.Replace(&quot;/&quot;, &quot;\\/&quot;).Replace(&quot;\&quot;&quot;, &quot;\\\&quot;&quot;).Replace(&quot;&apos;&quot;, &quot;\\&apos;&quot;);</span><br><span class="line">            return value;</span><br><span class="line">        &#125;</span><br><span class="line"></span><br><span class="line">        /// &lt;summary&gt;</span><br><span class="line">        /// 重置所有字段信息</span><br><span class="line">        /// &lt;/summary&gt;</span><br><span class="line">        protected void Clear()</span><br><span class="line">        &#123;</span><br><span class="line">            this.m_field = &quot;*&quot;;</span><br><span class="line">            this.m_join.Clear();</span><br><span class="line">            this.m_union.Clear();</span><br><span class="line">            this.m_data.Clear();</span><br><span class="line">            this.m_group = this.m_having = this.m_order = this.m_where = this.m_distinct = this.m_order = this.m_limit = this.LastSql = this.m_comment = &quot;&quot;;</span><br><span class="line">        &#125;</span><br><span class="line">        </span><br><span class="line">        /// &lt;summary&gt;</span><br><span class="line">        /// 打开连接(连接数据库)</span><br><span class="line">        /// &lt;/summary&gt;</span><br><span class="line">        /// &lt;param name=&quot;ConnectionString&quot;&gt;&lt;/param&gt;</span><br><span class="line">        /// &lt;returns&gt;&lt;/returns&gt;</span><br><span class="line">        public bool Open(string ConnectionString=null)</span><br><span class="line">        &#123;</span><br><span class="line">            if (ConnectionString != null)</span><br><span class="line">            &#123;</span><br><span class="line">                this.ConnectionString = ConnectionString;</span><br><span class="line">            &#125;</span><br><span class="line"></span><br><span class="line">            Connection = new MySqlConnection(this.ConnectionString);</span><br><span class="line">            try</span><br><span class="line">            &#123;</span><br><span class="line">                this.Connection.Open();</span><br><span class="line">                this.Command = this.Connection.CreateCommand();</span><br><span class="line">                return true;</span><br><span class="line">            &#125;</span><br><span class="line">            catch(MySqlException e)</span><br><span class="line">            &#123;</span><br><span class="line">                this.Exception = e.ToString();</span><br><span class="line">                return false;</span><br><span class="line">            &#125;</span><br><span class="line">        &#125;</span><br><span class="line"></span><br><span class="line">        /// &lt;summary&gt;</span><br><span class="line">        /// 关闭连接</span><br><span class="line">        /// &lt;/summary&gt;</span><br><span class="line">        public void Close()</span><br><span class="line">        &#123;</span><br><span class="line">            this.Connection.Close();</span><br><span class="line">        &#125;</span><br><span class="line"></span><br><span class="line">        /// &lt;summary&gt;</span><br><span class="line">        /// 执行SQL （只能执行 insert、update、delete），</span><br><span class="line">        /// &lt;/summary&gt;</span><br><span class="line">        /// &lt;param name=&quot;sql&quot;&gt;&lt;/param&gt;</span><br><span class="line">        /// &lt;returns&gt;&lt;/returns&gt;</span><br><span class="line">        public bool Execute(string sql = &quot;&quot;)</span><br><span class="line">        &#123;</span><br><span class="line">            this.Command.CommandText = sql;</span><br><span class="line">            this.AffectCount = this.Command.ExecuteNonQuery();</span><br><span class="line"></span><br><span class="line">            return this.AffectCount &gt; 0;</span><br><span class="line">        &#125;</span><br><span class="line"></span><br><span class="line">        /// &lt;summary&gt;</span><br><span class="line">        /// 查询第一列第一条记录</span><br><span class="line">        /// &lt;/summary&gt;</span><br><span class="line">        /// &lt;param name=&quot;sql&quot;&gt;&lt;/param&gt;</span><br><span class="line">        /// &lt;returns&gt;&lt;/returns&gt;</span><br><span class="line">        public object QueryOne(string sql)</span><br><span class="line">        &#123;</span><br><span class="line">            this.Command.CommandText = sql;</span><br><span class="line">            return this.Command.ExecuteScalar();</span><br><span class="line">        &#125;</span><br><span class="line"></span><br><span class="line">        /// &lt;summary&gt;</span><br><span class="line">        /// 查询SQL</span><br><span class="line">        /// &lt;/summary&gt;</span><br><span class="line">        /// &lt;param name=&quot;sql&quot;&gt;&lt;/param&gt;</span><br><span class="line">        /// &lt;returns&gt;&lt;/returns&gt;</span><br><span class="line">        public DataTable Query(string sql)</span><br><span class="line">        &#123;</span><br><span class="line">            this.Command.CommandText = sql;</span><br><span class="line">            MySqlDataAdapter adpter = new MySqlDataAdapter(this.Command);</span><br><span class="line">            DataTable dt = new DataTable(&quot;Table&quot;);</span><br><span class="line">            adpter.Fill(dt);</span><br><span class="line"></span><br><span class="line">            this.RecordCount = dt.Rows.Count;</span><br><span class="line">            this.FieldCount = dt.Columns.Count;</span><br><span class="line">            return dt;</span><br><span class="line">        &#125;</span><br><span class="line"></span><br><span class="line">        /// &lt;summary&gt;</span><br><span class="line">        /// 查询（select）</span><br><span class="line">        /// &lt;/summary&gt;</span><br><span class="line">        /// &lt;param name=&quot;table&quot;&gt;&lt;/param&gt;</span><br><span class="line">        /// &lt;returns&gt;&lt;/returns&gt;</span><br><span class="line">        public object Select(string table = null, string where = null)</span><br><span class="line">        &#123;</span><br><span class="line">            this.LastSql = this.GetSelectSql(table, where);</span><br><span class="line">            if (this.m_fetchSql)</span><br><span class="line">            &#123;</span><br><span class="line">                return this.LastSql;</span><br><span class="line">            &#125;</span><br><span class="line">            else</span><br><span class="line">            &#123;</span><br><span class="line">                DataTable data = this.Query(this.LastSql);</span><br><span class="line">                this.Clear();</span><br><span class="line">                return data;</span><br><span class="line">            &#125;</span><br><span class="line">        &#125;</span><br><span class="line"></span><br><span class="line">        /// &lt;summary&gt;</span><br><span class="line">        /// 新增记录（插入记录 insert）</span><br><span class="line">        /// &lt;/summary&gt;</span><br><span class="line">        /// &lt;param name=&quot;table&quot;&gt;&lt;/param&gt;</span><br><span class="line">        /// &lt;returns&gt;&lt;/returns&gt;</span><br><span class="line">        public object Add(string table = null)</span><br><span class="line">        &#123;</span><br><span class="line">            this.LastSql = this.GetInsertSql(table);</span><br><span class="line">            if (this.m_fetchSql)</span><br><span class="line">            &#123;</span><br><span class="line">                return this.LastSql;</span><br><span class="line">            &#125;</span><br><span class="line">            else</span><br><span class="line">            &#123;</span><br><span class="line">                bool ret = this.Execute(this.LastSql);</span><br><span class="line">                this.Clear();</span><br><span class="line">                return ret;</span><br><span class="line">            &#125;</span><br><span class="line">        &#125;</span><br><span class="line"></span><br><span class="line">        /// &lt;summary&gt;</span><br><span class="line">        /// 保存记录（更新记录 update）</span><br><span class="line">        /// &lt;/summary&gt;</span><br><span class="line">        /// &lt;param name=&quot;table&quot;&gt;&lt;/param&gt;</span><br><span class="line">        /// &lt;param name=&quot;where&quot;&gt;&lt;/param&gt;</span><br><span class="line">        /// &lt;returns&gt;&lt;/returns&gt;</span><br><span class="line">        public object Save(string table = null, string where = null)</span><br><span class="line">        &#123;</span><br><span class="line">            this.LastSql = this.GetUpdateSql(table, where);</span><br><span class="line">            if (this.m_fetchSql)</span><br><span class="line">            &#123;</span><br><span class="line">                return this.LastSql;</span><br><span class="line">            &#125;</span><br><span class="line">            else</span><br><span class="line">            &#123;</span><br><span class="line">                bool ret = this.Execute(this.LastSql);</span><br><span class="line">                this.Clear();</span><br><span class="line">                return ret;</span><br><span class="line">            &#125;</span><br><span class="line">        &#125;</span><br><span class="line"></span><br><span class="line">        /// &lt;summary&gt;</span><br><span class="line">        /// 删除记录 （delete）</span><br><span class="line">        /// &lt;/summary&gt;</span><br><span class="line">        /// &lt;param name=&quot;table&quot;&gt;&lt;/param&gt;</span><br><span class="line">        /// &lt;param name=&quot;where&quot;&gt;&lt;/param&gt;</span><br><span class="line">        /// &lt;returns&gt;&lt;/returns&gt;</span><br><span class="line">        public object Delete(string table = null, string where = null)</span><br><span class="line">        &#123;</span><br><span class="line">            this.LastSql = this.GetDeleteSql(table, where);</span><br><span class="line">            if (this.m_fetchSql)</span><br><span class="line">            &#123;</span><br><span class="line">                return this.LastSql;</span><br><span class="line">            &#125;</span><br><span class="line">            else</span><br><span class="line">            &#123;</span><br><span class="line">                bool ret = this.Execute(this.LastSql);</span><br><span class="line">                this.Clear();</span><br><span class="line">                return ret;</span><br><span class="line">            &#125;</span><br><span class="line">        &#125;</span><br><span class="line"></span><br><span class="line"></span><br><span class="line">        /// &lt;summary&gt;</span><br><span class="line">        /// 获取查询记录语句</span><br><span class="line">        /// &lt;/summary&gt;</span><br><span class="line">        /// &lt;param name=&quot;table&quot;&gt;&lt;/param&gt;</span><br><span class="line">        /// &lt;param name=&quot;where&quot;&gt;&lt;/param&gt;</span><br><span class="line">        /// &lt;returns&gt;&lt;/returns&gt;</span><br><span class="line">        protected string GetSelectSql(string table = null, string where = null)</span><br><span class="line">        &#123;</span><br><span class="line">            table = table ?? this.m_table;</span><br><span class="line">            where = &quot; WHERE &quot; + (where ?? this.m_where);</span><br><span class="line"></span><br><span class="line"></span><br><span class="line">            /*</span><br><span class="line">             * 0: 唯一</span><br><span class="line">             * 1:选取字段</span><br><span class="line">             * 2:表前缀</span><br><span class="line">             * 3:表名</span><br><span class="line">             * 4:表别称</span><br><span class="line">             * 5:join</span><br><span class="line">             * 6:where</span><br><span class="line">             * 7:group</span><br><span class="line">             * 8:having</span><br><span class="line">             * 9:order</span><br><span class="line">             * 10:limit</span><br><span class="line">             * 11:union</span><br><span class="line">             * 12:注释</span><br><span class="line">             */</span><br><span class="line">            this.LastSql = &quot;SELECT &#123;0&#125;&#123;1&#125; FROM `&#123;2&#125;&#123;3&#125;`&#123;4&#125;&#123;5&#125;&#123;6&#125;&#123;7&#125;&#123;8&#125;&#123;9&#125;&#123;10&#125;&#123;11&#125;&#123;12&#125;&quot;;</span><br><span class="line"></span><br><span class="line">            //join</span><br><span class="line">            string join = &quot;&quot;;</span><br><span class="line">            for (int i = 0; i &lt; this.m_join.Count; i++)</span><br><span class="line">            &#123;</span><br><span class="line">                join += &quot; &quot; + this.m_join[i];</span><br><span class="line">            &#125;</span><br><span class="line"></span><br><span class="line"></span><br><span class="line">            //union</span><br><span class="line">            string union = &quot;&quot;;</span><br><span class="line">            for (int i = 0; i &lt; this.m_union.Count; i++)</span><br><span class="line">            &#123;</span><br><span class="line">                union += &quot; &quot; + this.m_union[i];</span><br><span class="line">            &#125;</span><br><span class="line"></span><br><span class="line">            string sql = string.Format(this.LastSql, this.m_distinct, this.m_field, this.m_prefix, table, this.m_alias, join, where, this.m_group, this.m_having, this.m_order, this.m_limit, union, this.m_comment);</span><br><span class="line">            return sql;</span><br><span class="line">        &#125;</span><br><span class="line"></span><br><span class="line">        /// &lt;summary&gt;</span><br><span class="line">        /// 获取插入记录语句</span><br><span class="line">        /// &lt;/summary&gt;</span><br><span class="line">        /// &lt;param name=&quot;table&quot;&gt;&lt;/param&gt;</span><br><span class="line">        /// &lt;returns&gt;&lt;/returns&gt;</span><br><span class="line">        protected string GetInsertSql(string table = null)</span><br><span class="line">        &#123;</span><br><span class="line">            table = table ?? this.m_table;</span><br><span class="line"></span><br><span class="line">            string fields = &quot;&quot;, values = &quot;&quot;;</span><br><span class="line">            foreach (string key in this.m_data.Keys)</span><br><span class="line">            &#123;</span><br><span class="line">                fields += &quot;`&quot;+key + &quot;`, &quot;;</span><br><span class="line">                values += this.m_data[key] + &quot;, &quot;;</span><br><span class="line">            &#125;</span><br><span class="line"></span><br><span class="line">            if (fields.Length &gt; 3)</span><br><span class="line">            &#123;</span><br><span class="line">                fields = fields.Substring(0, fields.Length - 2);</span><br><span class="line">                values = values.Substring(0, values.Length - 2);</span><br><span class="line">            &#125;</span><br><span class="line"></span><br><span class="line">            string sql = string.Format(&quot;INSERT INTO `&#123;0&#125;&#123;1&#125;` (&#123;2&#125;) VALUES(&#123;3&#125;)&quot;, this.m_prefix, table, fields, values);</span><br><span class="line">            return sql;</span><br><span class="line">        &#125;</span><br><span class="line"></span><br><span class="line">        /// &lt;summary&gt;</span><br><span class="line">        /// 获取更新记录语句</span><br><span class="line">        /// &lt;/summary&gt;</span><br><span class="line">        /// &lt;param name=&quot;table&quot;&gt;&lt;/param&gt;</span><br><span class="line">        /// &lt;param name=&quot;where&quot;&gt;&lt;/param&gt;</span><br><span class="line">        /// &lt;returns&gt;&lt;/returns&gt;</span><br><span class="line">        protected string GetUpdateSql(string table = null, string where = null)</span><br><span class="line">        &#123;</span><br><span class="line"></span><br><span class="line">            table = table ?? this.m_table;</span><br><span class="line">            where = &quot; WHERE &quot; + (where ?? this.m_where);</span><br><span class="line"></span><br><span class="line">            string data = &quot;&quot;;</span><br><span class="line">            foreach (string key in this.m_data.Keys)</span><br><span class="line">            &#123;</span><br><span class="line">                data += &quot;`&quot; + key + &quot;` = &quot; + this.m_data[key] + &quot;, &quot;;</span><br><span class="line">            &#125;</span><br><span class="line"></span><br><span class="line">            if (data.Length &gt; 3)</span><br><span class="line">            &#123;</span><br><span class="line">                data = data.Substring(0, data.Length - 2);</span><br><span class="line">            &#125;</span><br><span class="line"></span><br><span class="line">            string sql = string.Format(&quot;UPDATE `&#123;0&#125;&#123;1&#125;` SET &#123;2&#125;&#123;3&#125;&quot;, this.m_prefix, table, data, where);</span><br><span class="line">            return sql;</span><br><span class="line">        &#125;</span><br><span class="line"></span><br><span class="line">        /// &lt;summary&gt;</span><br><span class="line">        /// 获取删除记录语句</span><br><span class="line">        /// &lt;/summary&gt;</span><br><span class="line">        /// &lt;param name=&quot;table&quot;&gt;&lt;/param&gt;</span><br><span class="line">        /// &lt;param name=&quot;where&quot;&gt;&lt;/param&gt;</span><br><span class="line">        /// &lt;returns&gt;&lt;/returns&gt;</span><br><span class="line">        protected string GetDeleteSql(string table = null, string where = null)</span><br><span class="line">        &#123;</span><br><span class="line">            table = table ?? this.m_table;</span><br><span class="line">            where = &quot; WHERE &quot; + (where ?? this.m_where);</span><br><span class="line"></span><br><span class="line">            string sql = string.Format(&quot;DELETE FROM `&#123;0&#125;&#123;1&#125;`&#123;2&#125;&#123;3&#125;&quot;, this.m_prefix, table, where, this.m_limit);</span><br><span class="line">            return sql;</span><br><span class="line">        &#125;</span><br><span class="line"></span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<blockquote>
<p>测试代码<br><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br></pre></td><td class="code"><pre><span class="line">using System;</span><br><span class="line">using System.Data;</span><br><span class="line">using System.Collections.Generic;</span><br><span class="line">using System.Collections;</span><br><span class="line">using System.Linq;</span><br><span class="line">using System.Text;</span><br><span class="line">using System.Threading.Tasks;</span><br><span class="line">using DbModel;</span><br><span class="line"></span><br><span class="line">namespace Test</span><br><span class="line">&#123;</span><br><span class="line">    class Program</span><br><span class="line">    &#123;</span><br><span class="line">        static void Main(string[] args)</span><br><span class="line">        &#123;</span><br><span class="line">            </span><br><span class="line">            DbModel.MySQL m = new DbModel.MySQL(&quot;user&quot;, &quot;table_&quot;);</span><br><span class="line">            m.ConnectionString = &quot;server=localhost;User Id=root;password=;Database=test;Charset=utf8&quot;;</span><br><span class="line">            m.Open();</span><br><span class="line">            //m.Open(&quot;server=localhost;User Id=root;password=;Database=test;Charset=utf8&quot;);</span><br><span class="line">            bool fetch = false; //返回SQL字符串, 并不执行</span><br><span class="line"></span><br><span class="line">            Console.WriteLine(&quot;新增测试&quot;);</span><br><span class="line">            m.Data(&quot;username&quot;, &quot;剑齿虎&quot;);</span><br><span class="line">            m.Data(&quot;password&quot;, &quot;123456&quot;);</span><br><span class="line">            m.Data(&quot;userid&quot;, null);</span><br><span class="line">            Console.WriteLine(m.FetchSql(fetch).Add());</span><br><span class="line"></span><br><span class="line">            m.Data(&quot;username&quot;, &quot;剑齿虎&quot;);</span><br><span class="line">            m.Data(&quot;password&quot;, &quot;123456&quot;);</span><br><span class="line">            m.Data(&quot;userid&quot;, 1);</span><br><span class="line">            Console.WriteLine(m.FetchSql(fetch).Add());</span><br><span class="line">            Console.WriteLine(&quot;影响行数:&quot; + m.AffectCount);</span><br><span class="line"></span><br><span class="line"></span><br><span class="line">            Console.WriteLine(&quot;\n\n修改测试&quot;);</span><br><span class="line">            m.Data(&quot;username&quot;, &quot;yuxiaobo64@gmail.com&quot;);</span><br><span class="line">            Console.WriteLine(m.FetchSql(fetch).Where(&quot;userid = 1&quot;).Save());</span><br><span class="line">            Console.WriteLine(&quot;影响行数:&quot; + m.AffectCount);</span><br><span class="line"></span><br><span class="line"></span><br><span class="line">            Console.WriteLine(&quot;\n\n查询测试&quot;);</span><br><span class="line">            DataTable dt = (DataTable)m.Where(&quot;1&quot;).Field(&quot;userid, username, password&quot;).Distinct(true).FetchSql(fetch).Order(&quot;username DESC&quot;).Select();</span><br><span class="line">            foreach (DataRow item in dt.Rows)</span><br><span class="line">            &#123;</span><br><span class="line">                Console.WriteLine(item[&quot;userid&quot;] + &quot;, &quot; + item[&quot;username&quot;] + &quot;, &quot; + item[&quot;password&quot;]);</span><br><span class="line">            &#125;</span><br><span class="line"></span><br><span class="line">            Console.WriteLine(&quot;记录数量:&quot; + m.RecordCount);</span><br><span class="line">            Console.WriteLine(&quot;字段数量:&quot; + m.FieldCount);</span><br><span class="line"></span><br><span class="line"></span><br><span class="line">            Console.WriteLine(&quot;\n\n删除测试&quot;);</span><br><span class="line">            Console.WriteLine(m.FetchSql(fetch).Where(&quot;password = &apos;123456&apos;&quot;).Delete());</span><br><span class="line">            Console.WriteLine(&quot;影响行数:&quot; + m.AffectCount);</span><br><span class="line"></span><br><span class="line">            Console.ReadKey();</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure></p>
</blockquote>
<blockquote>
<p>测试结果</p>
</blockquote>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br></pre></td><td class="code"><pre><span class="line">新增测试</span><br><span class="line">True</span><br><span class="line">True</span><br><span class="line">影响行数:1</span><br><span class="line"></span><br><span class="line"></span><br><span class="line">修改测试</span><br><span class="line">True</span><br><span class="line">影响行数:1</span><br><span class="line"></span><br><span class="line"></span><br><span class="line">查询测试</span><br><span class="line">107, 剑齿虎, 123456</span><br><span class="line">1, yuxiaobo64@gmail.com, 123456</span><br><span class="line">记录数量:2</span><br><span class="line">字段数量:3</span><br><span class="line"></span><br><span class="line"></span><br><span class="line">删除测试</span><br><span class="line">True</span><br><span class="line">影响行数:2</span><br></pre></td></tr></table></figure>
      
    </div>
    
    
    

    

    

    

    <footer class="post-footer">
      
        <div class="post-tags">
          
            <a href="/tags/C/" rel="tag"># C#</a>
          
            <a href="/tags/MySQLHelp/" rel="tag"># MySQLHelp</a>
          
        </div>
      

      
      
      

      
        <div class="post-nav">
          <div class="post-nav-next post-nav-item">
            
              <a href="/2017/fa102ada.html" rel="next" title="[易语言] 滴答表格挂接事件 : 事件编号">
                <i class="fa fa-chevron-left"></i> [易语言] 滴答表格挂接事件 : 事件编号
              </a>
            
          </div>

          <span class="post-nav-divider"></span>

          <div class="post-nav-prev post-nav-item">
            
              <a href="/2017/346f849c.html" rel="prev" title="C# winform 无边框窗口 移动">
                C# winform 无边框窗口 移动 <i class="fa fa-chevron-right"></i>
              </a>
            
          </div>
        </div>
      

      
      
    </footer>
  </div>
  
  
  
  </article>



    <div class="post-spread">
      
    </div>
  </div>


          </div>
          


          

  
    <div class="comments" id="comments">
      
        <div id="gitment-container"></div>
      
    </div>

  



        </div>
        
          
  
  <div class="sidebar-toggle">
    <div class="sidebar-toggle-line-wrap">
      <span class="sidebar-toggle-line sidebar-toggle-line-first"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-middle"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-last"></span>
    </div>
  </div>

  <aside id="sidebar" class="sidebar">
    
    <div class="sidebar-inner">

      

      
        <ul class="sidebar-nav motion-element">
          <li class="sidebar-nav-toc sidebar-nav-active" data-target="post-toc-wrap">
            文章目录
          </li>
          <li class="sidebar-nav-overview" data-target="site-overview-wrap">
            站点概览
          </li>
        </ul>
      

      <section class="site-overview-wrap sidebar-panel">
        <div class="site-overview">
          <div class="site-author motion-element" itemprop="author" itemscope itemtype="http://schema.org/Person">
            
              <img class="site-author-image" itemprop="image"
                src="/images/avatar.png"
                alt="余小波" />
            
              <p class="site-author-name" itemprop="name">余小波</p>
              <p class="site-description motion-element" itemprop="description"></p>
          </div>

          <nav class="site-state motion-element">

            
              <div class="site-state-item site-state-posts">
              
                <a href="/archives/">
              
                  <span class="site-state-item-count">34</span>
                  <span class="site-state-item-name">日志</span>
                </a>
              </div>
            

            
              
              
              <div class="site-state-item site-state-categories">
                <a href="/categories/index.html">
                  <span class="site-state-item-count">27</span>
                  <span class="site-state-item-name">分类</span>
                </a>
              </div>
            

            
              
              
              <div class="site-state-item site-state-tags">
                <a href="/tags/index.html">
                  <span class="site-state-item-count">57</span>
                  <span class="site-state-item-name">标签</span>
                </a>
              </div>
            

          </nav>

          

          
            <div class="links-of-author motion-element">
                
                  <span class="links-of-author-item">
                    <a href="https://github.com/edk24" target="_blank" title="GitHub">
                      
                        <i class="fa fa-fw fa-github"></i>GitHub</a>
                  </span>
                
                  <span class="links-of-author-item">
                    <a href="mailto:yuxiaobo64@gmail.com" target="_blank" title="E-Mail">
                      
                        <i class="fa fa-fw fa-envelope"></i>E-Mail</a>
                  </span>
                
                  <span class="links-of-author-item">
                    <a href="https://space.bilibili.com/10396559" target="_blank" title="Bilibili">
                      
                        <i class="fa fa-fw fa-play"></i>Bilibili</a>
                  </span>
                
                  <span class="links-of-author-item">
                    <a href="https://twitter.com/SabreCode" target="_blank" title="Twitter">
                      
                        <i class="fa fa-fw fa-twitter"></i>Twitter</a>
                  </span>
                
            </div>
          

          
          

          
          

          

        </div>
      </section>

      
      <!--noindex-->
        <section class="post-toc-wrap motion-element sidebar-panel sidebar-panel-active">
          <div class="post-toc">

            
              
            

            
              <div class="post-toc-content"><ol class="nav"><li class="nav-item nav-level-1"><a class="nav-link" href="#前言"><span class="nav-number">1.</span> <span class="nav-text">前言</span></a></li><li class="nav-item nav-level-1"><a class="nav-link" href="#数据表"><span class="nav-number">2.</span> <span class="nav-text">数据表</span></a></li><li class="nav-item nav-level-1"><a class="nav-link" href="#类库代码"><span class="nav-number">3.</span> <span class="nav-text">类库代码</span></a></li></ol></div>
            

          </div>
        </section>
      <!--/noindex-->
      

      
        <div class="back-to-top">
          <i class="fa fa-arrow-up"></i>
          
            <span id="scrollpercent"><span>0</span>%</span>
          
        </div>
      

    </div>
  </aside>


        
      </div>
    </main>

    <footer id="footer" class="footer">
      <div class="footer-inner">
        <div class="copyright">&copy; <span itemprop="copyrightYear">2019</span>
  <span class="with-love">
    <i class="fa fa-user"></i>
  </span>
  <span class="author" itemprop="copyrightHolder">余小波</span>

  
</div>









        







        
      </div>
    </footer>

    

    

  </div>

  

<script type="text/javascript">
  if (Object.prototype.toString.call(window.Promise) !== '[object Function]') {
    window.Promise = null;
  }
</script>









  












  
  
    <script type="text/javascript" src="https://cdn.bootcss.com/jquery/2.1.3/jquery.min.js"></script>
  

  
  
    <script type="text/javascript" src="https://cdn.bootcss.com/fastclick/1.0.6/fastclick.min.js"></script>
  

  
  
    <script type="text/javascript" src="https://cdn.bootcss.com/jquery_lazyload/1.9.7/jquery.lazyload.min.js"></script>
  

  
  
    <script type="text/javascript" src="https://cdn.bootcss.com/velocity/1.2.1/velocity.min.js"></script>
  

  
  
    <script type="text/javascript" src="https://cdn.bootcss.com/velocity/1.2.1/velocity.ui.min.js"></script>
  

  
  
    <script type="text/javascript" src="/lib/fancybox/source/jquery.fancybox.pack.js?v=2.1.5"></script>
  


  


  <script type="text/javascript" src="/js/src/utils.js?v=5.1.4"></script>

  <script type="text/javascript" src="/js/src/motion.js?v=5.1.4"></script>



  
  


  <script type="text/javascript" src="/js/src/affix.js?v=5.1.4"></script>

  <script type="text/javascript" src="/js/src/schemes/pisces.js?v=5.1.4"></script>



  
  <script type="text/javascript" src="/js/src/scrollspy.js?v=5.1.4"></script>
<script type="text/javascript" src="/js/src/post-details.js?v=5.1.4"></script>



  


  <script type="text/javascript" src="/js/src/bootstrap.js?v=5.1.4"></script>



  


  




	





  





  







<!-- LOCAL: You can save these files to your site and update links -->
    
        
        <link rel="stylesheet" href="https://aimingoo.github.io/gitmint/style/default.css">
        <script src="https://aimingoo.github.io/gitmint/dist/gitmint.browser.js"></script>
    
<!-- END LOCAL -->

    
      <style>
        a.gitment-editor-footer-tip { display: none; }
        .gitment-container.gitment-footer-container { display: none; }
      </style>
    

    
      <script type="text/javascript">
      function renderGitment(){
        var gitment = new Gitmint({
            id: window.location.pathname, 
            owner: 'edk24',
            repo: 'edk24.github.io',
            
            lang: "" || navigator.language || navigator.systemLanguage || navigator.userLanguage,
            
            oauth: {
            
            
                client_secret: 'fe67c38e961d9727108fbea7ad37a022c375449e',
            
                client_id: 'd555d8a2acc95a3ff25e'
            }});
        gitment.render('gitment-container');
      }

      
      renderGitment();
      
      </script>
    







  

  <script type="text/javascript">
    // Popup Window;
    var isfetched = false;
    var isXml = true;
    // Search DB path;
    var search_path = "search.xml";
    if (search_path.length === 0) {
      search_path = "search.xml";
    } else if (/json$/i.test(search_path)) {
      isXml = false;
    }
    var path = "/" + search_path;
    // monitor main search box;

    var onPopupClose = function (e) {
      $('.popup').hide();
      $('#local-search-input').val('');
      $('.search-result-list').remove();
      $('#no-result').remove();
      $(".local-search-pop-overlay").remove();
      $('body').css('overflow', '');
    }

    function proceedsearch() {
      $("body")
        .append('<div class="search-popup-overlay local-search-pop-overlay"></div>')
        .css('overflow', 'hidden');
      $('.search-popup-overlay').click(onPopupClose);
      $('.popup').toggle();
      var $localSearchInput = $('#local-search-input');
      $localSearchInput.attr("autocapitalize", "none");
      $localSearchInput.attr("autocorrect", "off");
      $localSearchInput.focus();
    }

    // search function;
    var searchFunc = function(path, search_id, content_id) {
      'use strict';

      // start loading animation
      $("body")
        .append('<div class="search-popup-overlay local-search-pop-overlay">' +
          '<div id="search-loading-icon">' +
          '<i class="fa fa-spinner fa-pulse fa-5x fa-fw"></i>' +
          '</div>' +
          '</div>')
        .css('overflow', 'hidden');
      $("#search-loading-icon").css('margin', '20% auto 0 auto').css('text-align', 'center');

      $.ajax({
        url: path,
        dataType: isXml ? "xml" : "json",
        async: true,
        success: function(res) {
          // get the contents from search data
          isfetched = true;
          $('.popup').detach().appendTo('.header-inner');
          var datas = isXml ? $("entry", res).map(function() {
            return {
              title: $("title", this).text(),
              content: $("content",this).text(),
              url: $("url" , this).text()
            };
          }).get() : res;
          var input = document.getElementById(search_id);
          var resultContent = document.getElementById(content_id);
          var inputEventFunction = function() {
            var searchText = input.value.trim().toLowerCase();
            var keywords = searchText.split(/[\s\-]+/);
            if (keywords.length > 1) {
              keywords.push(searchText);
            }
            var resultItems = [];
            if (searchText.length > 0) {
              // perform local searching
              datas.forEach(function(data) {
                var isMatch = false;
                var hitCount = 0;
                var searchTextCount = 0;
                var title = data.title.trim();
                var titleInLowerCase = title.toLowerCase();
                var content = data.content.trim().replace(/<[^>]+>/g,"");
                var contentInLowerCase = content.toLowerCase();
                var articleUrl = decodeURIComponent(data.url);
                var indexOfTitle = [];
                var indexOfContent = [];
                // only match articles with not empty titles
                if(title != '') {
                  keywords.forEach(function(keyword) {
                    function getIndexByWord(word, text, caseSensitive) {
                      var wordLen = word.length;
                      if (wordLen === 0) {
                        return [];
                      }
                      var startPosition = 0, position = [], index = [];
                      if (!caseSensitive) {
                        text = text.toLowerCase();
                        word = word.toLowerCase();
                      }
                      while ((position = text.indexOf(word, startPosition)) > -1) {
                        index.push({position: position, word: word});
                        startPosition = position + wordLen;
                      }
                      return index;
                    }

                    indexOfTitle = indexOfTitle.concat(getIndexByWord(keyword, titleInLowerCase, false));
                    indexOfContent = indexOfContent.concat(getIndexByWord(keyword, contentInLowerCase, false));
                  });
                  if (indexOfTitle.length > 0 || indexOfContent.length > 0) {
                    isMatch = true;
                    hitCount = indexOfTitle.length + indexOfContent.length;
                  }
                }

                // show search results

                if (isMatch) {
                  // sort index by position of keyword

                  [indexOfTitle, indexOfContent].forEach(function (index) {
                    index.sort(function (itemLeft, itemRight) {
                      if (itemRight.position !== itemLeft.position) {
                        return itemRight.position - itemLeft.position;
                      } else {
                        return itemLeft.word.length - itemRight.word.length;
                      }
                    });
                  });

                  // merge hits into slices

                  function mergeIntoSlice(text, start, end, index) {
                    var item = index[index.length - 1];
                    var position = item.position;
                    var word = item.word;
                    var hits = [];
                    var searchTextCountInSlice = 0;
                    while (position + word.length <= end && index.length != 0) {
                      if (word === searchText) {
                        searchTextCountInSlice++;
                      }
                      hits.push({position: position, length: word.length});
                      var wordEnd = position + word.length;

                      // move to next position of hit

                      index.pop();
                      while (index.length != 0) {
                        item = index[index.length - 1];
                        position = item.position;
                        word = item.word;
                        if (wordEnd > position) {
                          index.pop();
                        } else {
                          break;
                        }
                      }
                    }
                    searchTextCount += searchTextCountInSlice;
                    return {
                      hits: hits,
                      start: start,
                      end: end,
                      searchTextCount: searchTextCountInSlice
                    };
                  }

                  var slicesOfTitle = [];
                  if (indexOfTitle.length != 0) {
                    slicesOfTitle.push(mergeIntoSlice(title, 0, title.length, indexOfTitle));
                  }

                  var slicesOfContent = [];
                  while (indexOfContent.length != 0) {
                    var item = indexOfContent[indexOfContent.length - 1];
                    var position = item.position;
                    var word = item.word;
                    // cut out 100 characters
                    var start = position - 20;
                    var end = position + 80;
                    if(start < 0){
                      start = 0;
                    }
                    if (end < position + word.length) {
                      end = position + word.length;
                    }
                    if(end > content.length){
                      end = content.length;
                    }
                    slicesOfContent.push(mergeIntoSlice(content, start, end, indexOfContent));
                  }

                  // sort slices in content by search text's count and hits' count

                  slicesOfContent.sort(function (sliceLeft, sliceRight) {
                    if (sliceLeft.searchTextCount !== sliceRight.searchTextCount) {
                      return sliceRight.searchTextCount - sliceLeft.searchTextCount;
                    } else if (sliceLeft.hits.length !== sliceRight.hits.length) {
                      return sliceRight.hits.length - sliceLeft.hits.length;
                    } else {
                      return sliceLeft.start - sliceRight.start;
                    }
                  });

                  // select top N slices in content

                  var upperBound = parseInt('1');
                  if (upperBound >= 0) {
                    slicesOfContent = slicesOfContent.slice(0, upperBound);
                  }

                  // highlight title and content

                  function highlightKeyword(text, slice) {
                    var result = '';
                    var prevEnd = slice.start;
                    slice.hits.forEach(function (hit) {
                      result += text.substring(prevEnd, hit.position);
                      var end = hit.position + hit.length;
                      result += '<b class="search-keyword">' + text.substring(hit.position, end) + '</b>';
                      prevEnd = end;
                    });
                    result += text.substring(prevEnd, slice.end);
                    return result;
                  }

                  var resultItem = '';

                  if (slicesOfTitle.length != 0) {
                    resultItem += "<li><a href='" + articleUrl + "' class='search-result-title'>" + highlightKeyword(title, slicesOfTitle[0]) + "</a>";
                  } else {
                    resultItem += "<li><a href='" + articleUrl + "' class='search-result-title'>" + title + "</a>";
                  }

                  slicesOfContent.forEach(function (slice) {
                    resultItem += "<a href='" + articleUrl + "'>" +
                      "<p class=\"search-result\">" + highlightKeyword(content, slice) +
                      "...</p>" + "</a>";
                  });

                  resultItem += "</li>";
                  resultItems.push({
                    item: resultItem,
                    searchTextCount: searchTextCount,
                    hitCount: hitCount,
                    id: resultItems.length
                  });
                }
              })
            };
            if (keywords.length === 1 && keywords[0] === "") {
              resultContent.innerHTML = '<div id="no-result"><i class="fa fa-search fa-5x" /></div>'
            } else if (resultItems.length === 0) {
              resultContent.innerHTML = '<div id="no-result"><i class="fa fa-frown-o fa-5x" /></div>'
            } else {
              resultItems.sort(function (resultLeft, resultRight) {
                if (resultLeft.searchTextCount !== resultRight.searchTextCount) {
                  return resultRight.searchTextCount - resultLeft.searchTextCount;
                } else if (resultLeft.hitCount !== resultRight.hitCount) {
                  return resultRight.hitCount - resultLeft.hitCount;
                } else {
                  return resultRight.id - resultLeft.id;
                }
              });
              var searchResultList = '<ul class=\"search-result-list\">';
              resultItems.forEach(function (result) {
                searchResultList += result.item;
              })
              searchResultList += "</ul>";
              resultContent.innerHTML = searchResultList;
            }
          }

          if ('auto' === 'auto') {
            input.addEventListener('input', inputEventFunction);
          } else {
            $('.search-icon').click(inputEventFunction);
            input.addEventListener('keypress', function (event) {
              if (event.keyCode === 13) {
                inputEventFunction();
              }
            });
          }

          // remove loading animation
          $(".local-search-pop-overlay").remove();
          $('body').css('overflow', '');

          proceedsearch();
        }
      });
    }

    // handle and trigger popup window;
    $('.popup-trigger').click(function(e) {
      e.stopPropagation();
      if (isfetched === false) {
        searchFunc(path, 'local-search-input', 'local-search-result');
      } else {
        proceedsearch();
      };
    });

    $('.popup-btn-close').click(onPopupClose);
    $('.popup').click(function(e){
      e.stopPropagation();
    });
    $(document).on('keyup', function (event) {
      var shouldDismissSearchPopup = event.which === 27 &&
        $('.search-popup').is(':visible');
      if (shouldDismissSearchPopup) {
        onPopupClose();
      }
    });
  </script>





  

  

  

  
  

  

  

  

</body>
</html>
